var sumResult;

function RollDice(noOfDices){
	//$("#dice_table > *").remove();
		
	var table = $("<table>");
	table.attr("id","dice_table")
	.attr("border","0");
	
	var trow = $("<tr>");
	for(var i=1;i<=noOfDices;i++){
		$("<td>").attr("id","d"+i)
		.css("backgroundColor","white")
		.attr("class","roll_dice")		
		.appendTo(trow);
	} 
	
	var table_width = noOfDices * 63;
	var table_height = 64;
	
	var backgroundLeft = (window.innerWidth - table_width) / 2;
	var backgroundTop = (window.innerHeight - table_height) / 2;
	
	var centerDiv = $("<div>");
	centerDiv.attr("id","centerDiv")
	.css("position","absolute")
	.css("left",backgroundLeft)
	.css("top",backgroundTop)
	.css("z-Index",11);
				
	trow.appendTo(table);
	table.appendTo(centerDiv);
	
	centerDiv.appendTo($("body"));
	
	setTimeout("sumResult = AfterRoll();",2000);
	
	return sumResult;
}


function showBackground(){
	var background = document.getElementById('background');
	background.style.visibility='hidden';
	
	// REMOVE THE CENTER DIV WHEN CLICK ON THE SHELL
	var removeCenterDiv = $("#centerDiv");
	removeCenterDiv.remove();

	window.removeEventListener("DOMMouseWheel", cancel, false);
}

function showHiddenBackground(){
	var background = $("#background");
	background
	.width(window.innerWidth)
	.height(window.innerHeight)
	.attr("left",window.pageXOffset) 
	.attr("top",window.pageYOffset)
	.css("visibility","visible");

	
	window.addEventListener("DOMMouseWheel", cancel, false); 
}

function AfterRoll(){
	var roll;
	var noOfDices = $("#no_of_dices").val();
	var result=0;		
	for(var j=1;j<=noOfDices;j++){
		roll = Math.round(Math.random() * 6) % 6 + 1;
		$("#d"+j).attr("class","d"+roll);
		console.debug(roll);
		result = result + roll;
	}
	return result;
}

function getSumResult(){
	return sumResult;
}

// PREVENT SCROLL BAR
function cancel(event){ 
	var delta = 0;
       if (!event) /* For IE. */
               event = window.event;
       if (event.wheelDelta) { /* IE/Opera. */
               delta = event.wheelDelta/120;
               /** In Opera 9, delta differs in sign as compared to IE.
                */
               if (window.opera)
                       delta = -delta;
       } else if (event.detail) { /** Mozilla case. */
               /** In Mozilla, sign of delta is different than in IE.
                * Also, delta is multiple of 3.
                */
               delta = -event.detail/3;
       }
       /** If delta is nonzero, handle it.
        * Basically, delta is now positive if wheel was scrolled up,
        * and negative, if wheel was scrolled down.
        */
       if (delta)
               handle(delta);
       /** Prevent default actions caused by mouse wheel.
        * That might be ugly, but we handle scrolls somehow
        * anyway, so don't bother here..
        */
       if (event.preventDefault)
               event.preventDefault();
	event.returnValue = false; 
}